<?php

class CaseController extends Yaf_Controller_Abstract
{
    private $caseModel;
    private $sessoin;
    private $imageProcess;
    public function init()
    {
        $this->caseModel = new CaseModel();
        $this->session = Yaf_Session::getInstance();
        $this->imageProcess = new ImageProcessor();
    }
    public function indexAction()
    {
        $case = $this->caseModel->fetchCases();
        $this->getView()->assign('case',$case);
    }
    public function createAction()
    {
        
    }
    public function modifyAction()
    {
        $case = array();
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
            $case = $this->caseModel->fetchCaseById($id);
        $this->getView()->assign('case',$case[0]);
    }
   
    public function insertAction()
    {
        $type_id = intval($this->getRequest()->getPost('type'));
        if ($type_id!=0)
        {
            $detailImage = $this->imageProcess->upload_multi_image('case_image','detailImage');
            $filename = $this->imageProcess->upload_image('bg_image','image');
            if ($filename)
            {
                $this->imageProcess->thumbnail('bg_image',$filename,199,150);
                $name_ch = $this->getRequest()->getPost('name_ch');
                $name_en = $this->getRequest()->getPost('name_en');
                $show = $this->getRequest()->getPost('show');
                $place = $this->getRequest()->getPost('place');
                $area = $this->getRequest()->getPost('area');
                $management = $this->getRequest()->getPost('management');
                $design = $this->getRequest()->getPost('design');
                $fabrication = $this->getRequest()->getPost('fabrication');
                $time = $this->getRequest()->getPost('time');
                $detail = $this->getRequest()->getPost('detail');
                $data = array(
                    'name_ch' => $name_ch,
                    'name_en' => $name_en,
                    'show' => $show,
                    'place' => $place,
                    'area' => $area,
                    'management' => $management,
                    'design' => $design,
                    'fabrication' => $fabrication,
                    'time' => $time,
                    'bg_image' => $filename,
                    'detail' => $detail
                );
                $last_id = $this->caseModel->insertCase($data);
                $relation = array(
                    'case_id' =>$last_id,
                    'case_type_id' => $type_id
                );
                $this->caseModel->insertCaseType($relation);
                for($i=0;$i<count($detailImage);$i++)
                {
                    $caseImage = array(
                        'case_id' => $last_id,
                        'filename' => $detailImage[$i]
                    );
                    $this->caseModel->insertCaseImage($caseImage);
                }
            }
        }
        $this->forward('index');
        return FALSE;
    }
    public function recommandListAction()
    {
        $recommand_case = $this->caseModel->fetchRecommandCase();
        $this->getView()->assign('recommand_case',$recommand_case);
    }
    public function recommandOrderAction()
    {
        $order = $this->getRequest()->getPost('order');
        $recommand_case = $this->caseModel->fetchRecommandCase();
        for($i=0;$i<count($recommand_case);$i++)
        {
            if ($order[$i])
                $this->caseModel->updateCase(array('recommand'=>$order[$i]),array('id'=>$recommand_case[$i]['id']));
        }
        $this->forward('recommandlist');
        return FALSE;
    }
    public function caseOrderAction()
    {
        $order = $this->getRequest()->getPost('order');
        $case = $this->caseModel->fetchCases();
        for($i=0;$i<count($case);$i++)
        {
            if ($order[$i])
                $this->caseModel->updateCase(array('orderid'=>$order[$i]),array('id'=>$case[$i]['id']));
        }
        $this->forward('index');
        return FALSE;
    }
    public function addRecommandAction()
    {
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $this->caseModel->updateCase(array('recommand'=>1),array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
    public function deleteRecommandAction()
    {
       $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $this->caseModel->updateCase(array('recommand'=>0),array('id'=>$id));
        } 
        $this->forward('recommandList');
        return FALSE;
    }
    public function updateAction()
    {
        $id = intval($this->getRequest()->getPost('id'));
        if ($id)
        {
            $name_ch = $this->getRequest()->getPost('name_ch');
            $name_en = $this->getRequest()->getPost('name_en');
            $show = $this->getRequest()->getPost('show');
            $place = $this->getRequest()->getPost('place');
            $area = $this->getRequest()->getPost('area');
            $management = $this->getRequest()->getPost('management');
            $design = $this->getRequest()->getPost('design');
            $fabrication = $this->getRequest()->getPost('fabrication');
            $time = $this->getRequest()->getPost('time');
            $detail = $this->getRequest()->getPost('detail');
            $data = array(
                'name_ch' => $name_ch,
                'name_en' => $name_en,
                'show' => $show,
                'place' => $place,
                'area' => $area,
                'management' => $management,
                'design' => $design,
                'fabrication' => $fabrication,
                'time' => $time,
                'detail' => $detail
            );
            $this->caseModel->updateCase($data,array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
    public function deleteAction()
    {
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $case = $this->caseModel->fetchCaseById($id);
            unlink('../bg_image/'.$case[0]['bg_image']);
            unlink('../bg_image/s_'.$case[0]['bg_image']);
            $detailImage = $this->caseModel->fetchCaseDetailImageById($id);
            for($i=0;$i<count($detailImage);$i++)
                unlink ('../case_image/'.$detailImage[$i]['filename']);
            $this->caseModel->delete('case_image',array('case_id'=>$id));
            $this->caseModel->delete('case_relation',array('case_id'=>$id));
            $this->caseModel->delete('cases',array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
}
?>
